from datetime import datetime
import datetime
import time
from django.views import View
# Create your views here.
from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.http import JsonResponse

from cwd_articles.views import PageNum
from xj_users.models import Gathering
from xiu_event.serializers import EventSerializer, EventSerializerSimple

# /gatherings/
# class EventsView(View):
#     def get(self, request):
#         events = Gathering.objects.all()
#         count = Gathering.objects.count()
#         next = 'null'
#         previous = 'null'
#         results = []
#         users = {}
#
#         for event in events:
#             usersall = event.users
#             ac = len(usersall)
#             for a in range(ac):
#                 users["a"] = usersall(a)
#             id = event.id
#             image = event.image
#             name = event.name
#             city = event.city
#             starttime = event.starttime
#             endrolltime = event.endrolltime
#
#             result = { "id" : id ,
#                        "image": image,
#                        "name":name,
#                        "city":city,
#                        "starttime":starttime,
#                        "endrolltime":endrolltime,
#                        "users":users,
#
#             }
#             results.append(result)
#         data = {"count" : count,
#                 "next": next,
#                 "previous": previous,
#                 "results": results,
#
#
#         }
#         return JsonResponse (data)

# 获取所有活动

class EventsView(ListAPIView):
    serializer_class = EventSerializerSimple
    queryset = Gathering.objects.filter(state=1)
    pagination_class = PageNum



# /gatherings/{id}/活动详情
class EventView(RetrieveAPIView):
    serializer_class = EventSerializer
    queryset = Gathering.objects.filter(state=1)

# /gatherings/{id}/join/
    #不能省略到天
    # def Nowuserdate():
    #     Nowuserdate = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    #     Nowuserdate = datetime.datetime.strptime(Nowuserdate, '%Y-%m-%d').date()

    # return Nowuserdate
def Nowuserdate():
    Nowuserdate = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    Nowuserdate = datetime.datetime.strptime(Nowuserdate, '%Y-%m-%d %H:%M:%S')
    return Nowuserdate

class EventJoinView(GenericAPIView):
    queryset = Gathering.objects.filter(state=1).order_by('-starttime')
    permission_classes = [IsAuthenticated]

    def post(self, request,pk ):
        user = request.user
        gathering = Gathering.objects.get(id=pk)

        nowtime =  Nowuserdate()
        print(nowtime)

        event_serch=  Gathering.objects.get(id=pk)
        endtime = event_serch.endrolltime.strftime(('%Y-%m-%d %H:%M:%S'))
        endtime = datetime.datetime.strptime(endtime, '%Y-%m-%d %H:%M:%S')

        print(endtime,type(endtime))

        if endtime < nowtime:
            return Response({'success': False, 'message': '超时'}, status=400)
        else:
            if user in gathering.users.all():
                gathering.users.remove(user)
                gathering.save()
                return Response({'success': True, 'message': '已取消'})
            else:
                gathering.users.add(user)
                gathering.save()
                return Response({'success':True,'message':'已报名成功'})